#include "StdAfx.h"
#include "TGSvrNetMan.h"
#include "stdio.h"

int TGSvrNetMan::TcpAudienceDispatch(SOCKET hClientSocket,NET_PACKET_HEAD *pMsgHead,BYTE *pBuffer)
{
	int nRet = TG_ERR_SUCCESS;

	char szRetBuffer[256];
	memset(szRetBuffer,0,sizeof(szRetBuffer));

	NET_PACKET_HEAD *pRetMsgHead = (NET_PACKET_HEAD*)szRetBuffer;
	pRetMsgHead->nMsgType = pMsgHead->nMsgType + 1;

	SOCKADDR_IN addrPeer;
	int nAddrLen = sizeof(addrPeer);
	getpeername(hClientSocket, (PSOCKADDR) &addrPeer, &nAddrLen);

	char szPeerIP[128] = { 0 };
	sprintf(szPeerIP, "%s", inet_ntoa(addrPeer.sin_addr));

#ifdef dkq_test_svrnet_listen
	((TGSvrNetMan*)this)->m_xLog.WriteLog(__FILE__,__LINE__,"tcp in nMsgType:%d,szPeerIP:%s",pMsgHead->nMsgType,szPeerIP);
#endif

	switch( pMsgHead->nMsgType )
	{
	case MSG_TYPE_GETANCHOR:
		{
			SendAllAnchorToUser(hClientSocket);
		}
		break;
	default:
		{
			nRet = TG_ERR_FAILURE;
		}
		break;
	}

#ifdef dkq_test_svrnet_listen
	((TGSvrNetMan*)this)->m_xLog.WriteLog(__FILE__,__LINE__,"tcp out nMsgType:%d,nRet:%d",pMsgHead->nMsgType,nRet);
#endif

	if( TG_ERR_SUCCESS != nRet )
	{
		pRetMsgHead->nFrameSize = 0;
		pRetMsgHead->nErrFlag   = nRet;

		DWORD dwRet = 0;
		TGSendDataS(hClientSocket,
			(char*)szRetBuffer,sizeof(NET_PACKET_HEAD),
			&dwRet,
			m_xAudienceListen.m_hWSASREvent,
			m_xAudienceListen.m_hWSAExitEvent,TG_WAITTIME);

		//Sleep(50);

	}
	TGShutDownSocket( hClientSocket );
	TGCloseSocket( hClientSocket );
	hClientSocket = NULL;
	return nRet;
}

int TGSvrNetMan::UdpAudienceDispatch(SOCKET hSocket,TG_UDP_ADDR *pAddr,NET_PACKET_HEAD *pMsgHead,BYTE *pBuffer)
{
	int nRet = TG_ERR_SUCCESS;

	char szRetBuffer[256];
	memset(szRetBuffer,0,sizeof(szRetBuffer));

	NET_PACKET_HEAD *pRetMsgHead = (NET_PACKET_HEAD*)szRetBuffer;
	pRetMsgHead->nMsgType = pMsgHead->nMsgType + 1;

	char szPeerIP[128] = { 0 };
	sprintf(szPeerIP, "%s", inet_ntoa(pAddr->xAddr.sin_addr));

#ifdef dkq_test_svrnet_listen
	((TGSvrNetMan*)this)->m_xLog.WriteLog(__FILE__,__LINE__,"udp in nMsgType:%d,szPeerIP:%s",pMsgHead->nMsgType,szPeerIP);
#endif

	switch( pMsgHead->nMsgType )
	{
	case MSG_TYPE_BROADCAST:
		{
		}
		break;
	default:
		{
			nRet = UdpAudienceCommand(hSocket,pAddr,pMsgHead,pBuffer);
		}
		break;
	}

#ifdef dkq_test_svrnet_listen
	((TGSvrNetMan*)this)->m_xLog.WriteLog(__FILE__,__LINE__,"udp out nMsgType:%d,nRet:%d",pMsgHead->nMsgType,nRet);
#endif

	if( TG_ERR_SUCCESS != nRet )
	{
		pRetMsgHead->nFrameSize = 0;
		pRetMsgHead->nErrFlag   = nRet;

		sendto( hSocket,
			(char*)szRetBuffer,sizeof(NET_PACKET_HEAD),
			0,
			(struct sockaddr*)&pAddr->xAddr,pAddr->nAddrLen );
	}

	return nRet;
}

int TGSvrNetMan::UdpAudienceCommand(SOCKET hSocket,TG_UDP_ADDR *pAddr,NET_PACKET_HEAD *pMsgHead,BYTE *pBuffer)
{
	int nRet = TG_ERR_SUCCESS;

	switch( pMsgHead->nMsgType )
	{
	case MSG_TYPE_TGD_CLIENT_ALIVE:
	case MSG_TYPE_TGD_RETRANS_PACKET:
	case MSG_TYPE_TGD_CHANGE_NET_STATE:
		{

		}
		break;
	default:
		{
			nRet = TG_ERR_FAILURE;
			break;
		}
	}

	return nRet;
}

void	TGSvrNetMan::SendAllAnchorToUser(SOCKET sock)
{
	int nCount = m_xSessionMan.GetSessionCount();
	UINT8* pAnchorList = NULL;
	DWORD dwRet = 0;
	if(nCount <= 0)
	{
		NET_PACKET_HEAD Packet;
		Packet.nMsgType = MSG_TYPE_ANCHOR;
		Packet.nFrameSize = 0;
		Packet.SetCRC();
		TGSendDataS(sock,
			(char*)&Packet, sizeof(NET_PACKET_HEAD),
			&dwRet,
			m_xAudienceListen.m_hWSASREvent,
			m_xAudienceListen.m_hWSAExitEvent,TG_WAITTIME);
		return;
	}

	pAnchorList = new UINT8[sizeof(NET_PACKET_HEAD) + nCount * (TG_USERNAME_LEN + sizeof(DWORD))];
	m_xSessionMan.GetAnchorList(pAnchorList + sizeof(NET_PACKET_HEAD), nCount);
	
	NET_PACKET_HEAD *pRetMsgHead = (NET_PACKET_HEAD*)pAnchorList;
	pRetMsgHead->nMsgType = MSG_TYPE_ANCHOR;
	pRetMsgHead->nFrameSize = nCount * (TG_USERNAME_LEN + sizeof(DWORD));
	pRetMsgHead->SetCRC();
	

	TGSendDataS(sock,
		(char*)pAnchorList,sizeof(NET_PACKET_HEAD) + nCount * (TG_USERNAME_LEN + sizeof(DWORD)),
		&dwRet,
		m_xAudienceListen.m_hWSASREvent,
		m_xAudienceListen.m_hWSAExitEvent,TG_WAITTIME);
	MySafeDeleteMAB(pAnchorList);
}
